package com.api.syh56.shengyunhui.controller;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.Car;
import com.api.syh56.shengyunhui.service.AddressService;
import com.api.syh56.shengyunhui.service.CarService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("car")
public class CarController {

    @Autowired
    private CarService carService;

    /**
     * 车辆插入
     * @param car
     * @return
     */
    @PostMapping("insert")
    public CommonResult<Object> addNew(@RequestBody Car car){
//        address.setUid(Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode())));
        carService.addNewCar(car);
        return CommonResult.success(car);
    }

    /**
     * 车辆删除
     * @param car
     * @return
     */
    @PostMapping("delete")
    public CommonResult<Object> delete(@RequestBody Car car){
        carService.removeCar(car);

        return CommonResult.success(null);
    }

    /**
     * 车辆更新
     * @param car
     * @return
     */
    @PostMapping("update")
    public CommonResult<Object> update(@RequestBody Car car){
        carService.updateCar(car);
        return CommonResult.success(null);
    }

    /**
     * 车辆列表
     * @return
     */
    @PostMapping("selectAll")
    public CommonResult<Object> selectAll(){
        List<Car> carList = carService.list();
        return CommonResult.success(carList);
    }

    /**
     * 车辆id选择
     * @param car
     * @return
     */
    @PostMapping("selectById")
    public CommonResult<Object> selectById(@RequestBody Car car){
        return CommonResult.success(carService.selectById(car.getId()));
    }

    /**
     * 分页查询车辆
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping (value = "selectPageByuid",method = RequestMethod.POST)
    public CommonResult<CommonPage<Car>>  selectPageByuid(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        String id = BaseUserInfo.get(UserInfoEnu.id.getCode());
        Page<Car> carPage = carService.selectPageByuid(Long.valueOf(id),pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(carPage));
    }


}
